package read.prjx.sum;

import read.prjx.sum.data.DimEn;
import read.prjx.sum.data.PrJxInfo;
import read.prjx.sum.data.SumData;
import read.prjx.sum.data.SumInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Sum {
    private SumData sumData;

    public SumData getSumData() {
        return sumData;
    }

    public void setSumData(String sumDataStr) {
        SumData sumDat = new SumData();
        SumInfo sumInfo = new SumInfo();
        PrJxInfo prJxInfo = new PrJxInfo();
        List<DimEn> dimEnArrayList = new ArrayList<>();
        List<String> sumStrArr = Arrays.asList(sumDataStr.split("@"));
        List<String> sumInfoStr = Arrays.asList(sumStrArr.get(0).split(System.getProperty("line.separator")));
        for (String s : sumInfoStr) {
            if (s.contains("CVERSION")) {
                sumInfo.setCverSion(s.split("=", 2)[1]);
            }
            if (s.contains("MEASUREMENT")) {
                sumInfo.setMeasuRementl(s.split("=", 2)[1]);
            }
            if (s.contains("DATE")) {
                sumInfo.setSumDate(s.split("=", 2)[1]);
            }
            if (s.contains("VENDOR")) {
                sumInfo.setVenDor(s.split("=", 2)[1]);
            }
        }
        sumInfoStr = Arrays.asList(sumStrArr.get(1).split(System.getProperty("line.separator")));

        for (String s : sumInfoStr) {
            if (s.contains("QUANTITY")) {
                prJxInfo.setQuanTity(s.split("=", 2)[1]);
            }
            if (s.contains("DIMENX")) {
                prJxInfo.setDimeNx(s.split("=", 2)[1]);
            }
            if (s.contains("PATTERNX")) {
                prJxInfo.setPattErnx(s.split("=", 2)[1]);
            }
        }


        for (String s : sumStrArr) {
            for (int i = 0; i < Integer.parseInt(prJxInfo.getDimeNx()); i++) {

                if (s.contains("DIMEN" + (i + 1))){
                    DimEn dimEn = new DimEn();
                    List<String> dimStrArr = Arrays.asList(s.split(System.getProperty("line.separator")));
                    for (String simStr : dimStrArr)
                    {
                        if (simStr.contains("WIDTH")){
                            dimEn.setWidTh(simStr.split("=", 2)[1]);
                        }
                        if (simStr.contains("HEIGHT")){
                            dimEn.setHeigTh(simStr.split("=", 2)[1]);
                        }
                        if (simStr.contains("GTHICKNESS")){
                            dimEn.setGthiCkness(simStr.split("=", 2)[1]);
                        }
                        if (simStr.contains("GCLR")){
                            dimEn.setGclr(simStr.split("=", 2)[1]);
                        }
                        if (simStr.contains("QNTY")){
                            dimEn.setQnTy(simStr.split("=", 2)[1]);
                        }
                        if (simStr.contains("OTHERS")){
                            dimEn.setOtheRs(simStr.split("=", 2)[1]);
                        }
                    }
                    dimEnArrayList.add(dimEn);
                }
            }
        }
        sumDat.setSumInfo(sumInfo);
        sumDat.setPrJxInfo(prJxInfo);
        sumDat.setDimEn(dimEnArrayList);
        this.sumData = sumDat;
    }

    @Override
    public String toString() {
        return "Sum{" +
                "sumData=" + sumData +
                '}';
    }
}
